#include <bits/stdc++.h>

#define eb emplace_back
#define ep emplace
#define fi first
#define se second
#define in read<int>()
#define lin read<ll>()
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

using ll = long long;
using db = double;
using pii = pair < int, int >;
using vec = vector < int >;
using veg = vector < pii >;

template < typename T > T read() {
	T x = 0; bool f = 0; char ch = getchar();
	while(!isdigit(ch)) f |= ch == '-', ch = getchar();
	while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar();
	return f ? -x : x;
}

template < typename T > void chkmax(T &x, const T &y) { x = x > y ? x : y; }
template < typename T > void chkmin(T &x, const T &y) { x = x < y ? x : y; }

const int N = 1e6 + 10;

vector < tuple < int, int, int > > G[N];
int stk[N << 1], top, n, fa[N], to[N];
ll s[N << 1], mxd[N], ans;
bool vis[N], ban[N];
bool fl;

void dfs(int x, int p) {
	vis[x] = true; 
	for(auto v : G[x])
		if(get<2>(v) ^ p) {
			int y, w, id; tie(y, w, id) = v;
			if(!vis[y]) to[y] = w, fa[y] = x, dfs(y, id);
			else {
				fl = true;
				int u = x, lst = w;
				while(u != y) {
					stk[++top] = u; s[top] = lst; ban[u] = true; lst = to[u]; u = fa[u];
				} stk[++top] = u; s[top] = lst; ban[u] = true;
				rep(i, 1, top) stk[i + top] = stk[i], s[i + top] = s[i];
				rep(i, 1, top << 1) s[i] += s[i - 1];
			} if(fl) return;
		}
}

ll diam;

void tdfs(int x, int p) {
	vis[x] = true; mxd[x] = 0;
	for(auto y : G[x])
		if(get<0>(y) ^ p && !ban[get<0>(y)])
			tdfs(get<0>(y), x), chkmax(diam, mxd[x] + mxd[get<0>(y)] + get<1>(y)), chkmax(mxd[x], mxd[get<0>(y)] + get<1>(y));
}

int tstk[N << 1], fr, ed;

void solve(int x) {
	fl = false; top = 0; diam = 0; 
	dfs(x, 0); rep(i, 1, top) tdfs(stk[i], 0);
	fr = 1; ed = 0; ll res = 0;
	rep(i, 1, top << 1) {
		while(fr <= ed && tstk[fr] <= i - top) fr++;
		if(fr <= ed) chkmax(res, mxd[stk[i]] + s[i] - s[tstk[fr]] + mxd[stk[tstk[fr]]]);
		while(fr <= ed && mxd[stk[i]] - s[i] >= mxd[stk[tstk[ed]]] - s[tstk[ed]]) ed--; tstk[++ed] = i; 
	} ans += max(diam, res);
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("1.in", "r", stdin);
#endif
	n = in;
	rep(i, 1, n) { int x = in, w = in; G[i].eb(x, w, i); G[x].eb(i, w, i); }
	rep(i, 1, n) if(!vis[i]) solve(i); printf("%lld\n", ans);
	return 0;
}
